package com.shop2cn.iapi.productprice.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.productprice.parameter.GetMchProductPriceListBean;
import com.shop2cn.iapi.productprice.service.GetMchProductPriceListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
/**
*商家采购价格列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMchProductPriceList{
	private static GetMchProductPriceListBean getmchproductpricelistBean;
	private static GetMchProductPriceListCall getmchproductpricelistCall;
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	//渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家采购价格列表Test");
	}

	@Before
	public void caseUp() {
		getmchproductpricelistBean=new GetMchProductPriceListBean();
		getmchproductpricelistCall=new GetMchProductPriceListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmchproductpricelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchProductPriceList_001() {
		Logger.start(true,"普通代理查询采购价格，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,sellerId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			List<String> catalogIds = new ArrayList<>();
			ymtCatalogsIWapper.selectCatalogsByProductId(productId).forEach(p->{
				catalogIds.add(p.getsCatalogId());
			});
			
			getmchproductpricelistBean.setMchId(agentUserId1);
			getmchproductpricelistBean.setProductIdList(Arrays.asList(productId));
			getmchproductpricelistCall.setData(getmchproductpricelistBean);
			getmchproductpricelistCall.callService();			
			Logger.verifyEquals("200", getmchproductpricelistCall.getString("code"), "验证code");
			JsonArray data = getmchproductpricelistCall.getJsonObject().getAsJsonArray("data");
			Logger.verifyEquals(productId, data.get(0).getAsJsonObject().get("productId"), "验证productid");
			Logger.verifyEquals(productId, data.get(1).getAsJsonObject().get("productId"), "验证productid");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchProductPriceList_002() {
		Logger.start(true,"渠道商查询采购价格，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogIds = new ArrayList<>();
			ymtCatalogsIWapper.selectCatalogsByProductId(productId).forEach(p->{
				catalogIds.add(p.getsCatalogId());
			});
			getmchproductpricelistBean.setMchId(sellerId);
			getmchproductpricelistBean.setProductIdList(Arrays.asList(productId));
			getmchproductpricelistCall.setData(getmchproductpricelistBean);
			getmchproductpricelistCall.callService();			
			Logger.verifyEquals("300", getmchproductpricelistCall.getString("code"), "验证code");
			Logger.verifyIsContains("无上级", getmchproductpricelistCall.getString("message"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchProductPriceList_003() {
		Logger.start(true,"商户id错误，查询失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogIds = new ArrayList<>();
			ymtCatalogsIWapper.selectCatalogsByProductId(productId).forEach(p->{
				catalogIds.add(p.getsCatalogId());
			});
			getmchproductpricelistBean.setMchId(1123);
			getmchproductpricelistBean.setProductIdList(Arrays.asList(productId));
			getmchproductpricelistCall.setData(getmchproductpricelistBean);
			getmchproductpricelistCall.callService();			
			Logger.verifyEquals("300", getmchproductpricelistCall.getString("code"), "验证code");
			Logger.verifyIsContains("无上级", getmchproductpricelistCall.getString("message"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchProductPriceList_004() {
		Logger.start(true,"代理商商品不属于自己，失败");
		try {
			getmchproductpricelistBean.setMchId(agentUserId1);
			getmchproductpricelistBean.setProductIdList(Arrays.asList("p123456"));
			getmchproductpricelistCall.setData(getmchproductpricelistBean);
			getmchproductpricelistCall.callService();			
			Logger.verifyEquals("300", getmchproductpricelistCall.getString("code"), "验证code");
			Logger.verifyIsContains("无法找到规格", getmchproductpricelistCall.getString("message"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}